package com.maxh.controller;


import com.maxh.annotation.PassToken;
import com.maxh.annotation.SuperAuthority;
import com.maxh.annotation.AdminLoginToken;
import com.maxh.annotation.TeacherLoginToken;
import com.maxh.pojo.Teacher;
import com.maxh.service.TeacherService;
import com.maxh.service.UserService;
import com.maxh.util.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class TeacherController {
    @Autowired
    TeacherService teacherService;
    @Autowired
    UserService userService;

    /*管理员获取讲师信息*/
    @AdminLoginToken
    @GetMapping("/training/user/teacher/list")
    public Object getTeacherList(){
        List teacherList =  teacherService.getTeacherList();
        System.out.println("管理员获取讲师信息："+teacherList);
        if(teacherList.size()>0){
            return new ReturnData(StatusCode.REQUEST_SUCCESS,teacherList ,"数据获取成功！");
        }
        return new ReturnData(StatusCode.REQUEST_SUCCESS,teacherList ,"数据为空！");
    }

    /*管理员根据用户姓名或工号查询用户信息*/
    @AdminLoginToken
    @GetMapping("/training/teacher/search")
    public Object searchTeacher(@RequestParam Map<String,Object> map){
        System.out.println(map);
        if(map==null) return new ReturnError(StatusCode.RETURN_ERROR,"关键字不能为空");

        List userList =  teacherService.searchTeacherList(map);
        System.out.println("管理员根据用户姓名或工号查询用户信息："+userList);
        if(userList.size()>0) return new ReturnData(StatusCode.REQUEST_SUCCESS,userList ,"数据查询成功！");
        else return new ReturnData(StatusCode.REQUEST_SUCCESS,"数据为空");
    }

    /*根据用户Id查询讲师信息*/
    @PassToken
    @GetMapping("/training/teacher/searchById")
    public Object searchTeacherById(@RequestParam Map<String,Object> map){
        System.out.println(map);
        if(map.get("id")==null) return new ReturnError(StatusCode.RETURN_ERROR,"ID不能为空");

        Teacher teacher = teacherService.searchTeacherById(map);
        System.out.println("管理员根据用户Id查询用户信息："+teacher);
        return new ReturnData(StatusCode.REQUEST_SUCCESS,teacher ,"数据获取完成！");
    }
    /*超级管理员根据讲师工号删除讲师信息*/
    @SuperAuthority
    @PostMapping("/training/user/teacher/delete")
    public Object deleteTeacherById(@RequestParam Map<String,Object> map){
        System.out.println(map);
        if(map.get("id")==null) return new ReturnError(StatusCode.RETURN_ERROR,"ID不能为空");

        Teacher teacher = teacherService.searchTeacherById(map);
        Boolean isDeleteStudent = teacherService.deleteTeacher(map.get("id")+"");

        if(isDeleteStudent) return new ReturnData(StatusCode.REQUEST_SUCCESS,teacher ,"删除成功！");
        else return new ReturnError(StatusCode.RETURN_ERROR,"删除失败！");
    }
    /*管理员添加讲师信息*/
    @AdminLoginToken
    @PostMapping("/training/user/teacher/add")
    public Object addTeacher(@RequestParam Map<String,Object> map){
        if(map.get("userNum")==null || map.get("username")==null || map.get("email")==null
                || map.get("phone")==null || map.get("degree")==null || map.get("title")==null){
            return new ReturnError(StatusCode.RETURN_ERROR,"必填信息不能为空");
        }
        //判断工号是否已经存在
        Map<String, Object> mapUserNum= new HashMap<String, Object>();
        mapUserNum.put("mapUserNum",map.get("userNum"));
        List userList =  teacherService.searchTeacherList(mapUserNum);
        if(userList.size()>0) return new ReturnError(StatusCode.RETURN_ERROR,"工号已存在，请重新输入！");

        /*获取当前时间*/
        map.put("status",1);
        String createTime = new ReturnUtil().nowTime();
        map.put("createTime",createTime);
        String passwordMD5 = MD5Util.getMD5("12345678");
        map.put("password",passwordMD5);
        System.out.println("管理员添加讲师信息："+map);

        Boolean isAddTeacher = teacherService.addTeacher(map);
        if(isAddTeacher){
            List teacher = teacherService.searchTeacherList(mapUserNum);
            System.out.println(teacher);
            return new ReturnData(StatusCode.REQUEST_SUCCESS,teacher ,"添加成功！");
        }else{
            return new ReturnError(StatusCode.RETURN_ERROR,"添加失败！");
        }
    }

    /*管理员修改讲师信息*/
    @AdminLoginToken
    @PostMapping("/training/user/teacher/update")
    public Object updateTeacher(@RequestParam Map<String,Object> map){
        System.out.println(map);
        if(map.get("id")==null) return new ReturnError(StatusCode.RETURN_ERROR,"ID不能为空");

        Boolean isUpdateTeacher = teacherService.updateTeacher(map);
        if(isUpdateTeacher){
            Map<String, Object> mapId= new HashMap<String, Object>();
            mapId.put("id",map.get("id"));
            Teacher teacherObj = teacherService.searchTeacherById(mapId);
            System.out.println("管理员更新讲师信息："+teacherObj);
            return new ReturnData(StatusCode.REQUEST_SUCCESS,teacherObj ,"修改成功！");
        }else{
            return new ReturnError(StatusCode.RETURN_ERROR,"修改失败！");
        }
    }

    /*讲师查询个人信息*/
    @TeacherLoginToken
    @GetMapping("/training/teacher/info")
    public Object TeacherInfo(){
        String id = TokenUtil.getTokenTeacherId();
        Map<String, Object> mapId= new HashMap<String, Object>();
        mapId.put("id",id);
        Teacher teacher = teacherService.searchTeacherById(mapId);
        System.out.println("讲师查询个人信息："+teacher);
        return new ReturnData(StatusCode.REQUEST_SUCCESS,teacher ,"数据获取成功！");
    }

    /*讲师修改个人信息*/
    @TeacherLoginToken
    @PostMapping("/training/teacher/info/update")
    public Object updateTeacherInfo(@RequestParam Map<String,Object> map){
        String id = TokenUtil.getTokenTeacherId();
        map.put("id",id);
        Boolean isUpdateTeacher = teacherService.updateTeacher(map);
        if(isUpdateTeacher){
            return new ReturnData(StatusCode.REQUEST_SUCCESS ,"修改成功！");
        }else{
            return new ReturnError(StatusCode.RETURN_ERROR,"修改失败！");
        }
    }

    /*讲师修改密码*/
    @TeacherLoginToken
    @PostMapping("/training/teacher/password/update")
    public Object UpdateTeacherPassword(@RequestParam("oldPassword") String oldPassword,@RequestParam("newPassword") String newPassword){
        if(oldPassword==null || newPassword==null) return new ReturnError(StatusCode.RETURN_ERROR,"新密码和原密码都不能为空");
        //获取讲师Id
        String id = TokenUtil.getTokenTeacherId();
        Map<String, Object> mapId= new HashMap<String, Object>();
        mapId.put("id",id);
        Teacher teacher = teacherService.searchTeacherById(mapId);
        String oldPassMD5 = MD5Util.getMD5(oldPassword);
        if(!teacher.getPassword().equals(oldPassMD5)){  return new ReturnError(StatusCode.RETURN_ERROR,"原密码错误！");}
        String newPassMD5 = MD5Util.getMD5(newPassword);
        Boolean isUpdate =  teacherService.updatePassword(id,newPassMD5);
        System.out.println("讲师修改密码："+isUpdate);
        if(isUpdate){
            return new ReturnData(StatusCode.REQUEST_SUCCESS ,"密码修改成功！");
        }else{
            return new ReturnError(StatusCode.RETURN_ERROR,"密码修改失败！");
        }
    }
    //学员获取讲师信息
    @GetMapping("/training/student/get/teacher/list")
    public Object userGetTeacher(){
        List teacherList =  teacherService.userGetTeacher();
        System.out.println("学员获取讲师信息："+teacherList);
        if(teacherList.size()>0){
            return new ReturnData(StatusCode.REQUEST_SUCCESS,teacherList ,"数据获取成功！");
        }
        return new ReturnData(StatusCode.REQUEST_SUCCESS,teacherList ,"数据为空！");
    }
}
